/**
 * ClassName: UserController
 * Package: org.example.controller
 * Description: 用户控制器-调用基于注解的UserService
 *
 * @Author BCXJ
 * @Create 2025/10/6 02:15
 * @Version 1.0
 * @Since 1.0
 */
package org.example.controller;

import org.example.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 用户控制器
 * 通过XML配置引用远程UserService服务
 */
@Controller
@RequestMapping("/user")
public class UserController {
    
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    
    /**
     * 通过Spring注入UserService
     * 实际是Dubbo的远程服务代理（通过XML配置引用）
     */
    @Resource
    private UserService userService;
    
    /**
     * 获取用户名接口
     * 
     * @param userId 用户ID
     * @return 用户名
     */
    @RequestMapping("/getUserName")
    @ResponseBody
    public String getUserName(@RequestParam("userId") Long userId) {
        logger.info("【Web消费者】收到请求: /user/getUserName, userId={}", userId);
        
        try {
            String username = userService.getUserName(userId);
            logger.info("【Web消费者】调用UserService成功，返回: {}", username);
            return "成功！用户名: " + username;
        } catch (Exception e) {
            logger.error("【Web消费者】调用UserService失败", e);
            return "失败！错误信息: " + e.getMessage();
        }
    }
    
    /**
     * 用户注册接口
     * 
     * @param username 用户名
     * @param password 密码
     * @return 注册结果
     */
    @RequestMapping("/register")
    @ResponseBody
    public String registerUser(@RequestParam("username") String username, 
                              @RequestParam("password") String password) {
        logger.info("【Web消费者】收到请求: /user/register, username={}", username);
        
        try {
            String result = userService.registerUser(username, password);
            logger.info("【Web消费者】调用UserService成功，返回: {}", result);
            return "成功！" + result;
        } catch (Exception e) {
            logger.error("【Web消费者】调用UserService失败", e);
            return "失败！错误信息: " + e.getMessage();
        }
    }
}

